

// 生成表单道具、表格道具
const createProps = (item, optsConfig, props) => {
  const [label, prop, type, clearable] = item;
  let subProp = { label, prop, clearable };
  if (type) {
    // 表单才有type
    subProp.type = type
    subProp.show = true
    Object.keys(optsConfig).length > 0 && addSelectOptions(prop, optsConfig, subProp)
  }
  props.push(subProp)
}
// 给select控件添加选项
const addSelectOptions = (prop, optsConfig, subProp) => {
  if (optsConfig[prop]) {
    const { options, optionFields } = optsConfig[prop]
    subProp.options = options
    subProp.optionFields = optionFields
  }
}
// 生成表单规则
const createRules = (item, kind, rules) => {
  const [label, prop] = item;
  let rule = { required: true, message: '请输入' + label, trigger: 'blur' }
  if (kind === 'search') {
    rule.required = false
  }
  rules[prop] = [rule]
}

// 生成表单数据模型
const createModel = (item, model) => {
  const [, prop] = item;
  // TODO：特殊控件需要处理类型
  model[prop] = ''
}

// core-fn 
export const modelHandler = (formConfig, kind, optsConfig) => {
  let props = [];
  let rules = {};
  let model = {};
  formConfig.forEach(item => {
    createProps(item, optsConfig, props)
    if (kind !== 'table') {
      createRules(item, kind, rules)
      createModel(item, model)
    }
  })
  return {
    props,
    rules,
    model
  }
}